package com.qf.listener;

import com.qf.Constant.RabbitMqConstant;
import com.qf.entity.Goods;
import com.qf.service.SearchService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 你的名字
 * @Date: 2021/07/12/20:55
 * @Description:
 */
@Component
@Slf4j
public class GoodsListener {
    @Autowired
    private SearchService searchService;
    @RabbitListener(queues = RabbitMqConstant.GOOD_ES_QUEUE)
    public void addGoodsToEs(Goods goods , Channel channel, Message message) throws IOException {
        try {
            System.out.println("添加到es的数据:"+goods.getId());
            searchService.add(goods);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
        }

    }
}
